using EasyElasticLogger.NETFramework.ES.Configuration;
using EasyElasticLogger.NETFramework.ES.Logging;
using System;
using System.Collections.Generic;

namespace EasyElasticLogger.NETFramework.ES.Tests
{
    /// <summary>
    /// 使用示例演示如何在实际项目中使用静态日志记录器
    /// </summary>
    public class UsageExample
    {
        /// <summary>
        /// 应用程序入口点示例
        /// </summary>
        public static void ApplicationStartup()
        {
            // 在应用程序启动时初始化日志记录器
            var config = new LoggerConfiguration
            {
                Enabled = true,
                IndexPrefix = "myapp-log",
                ApplicationName = "MyApplication",
                Cluster = new ClusterConfiguration
                {
                    Nodes = new List<string> { "http://localhost:9200" },
                    Username = "elastic",
                    Password = "password"
                }
            };

            // 初始化静态日志记录器
            ElasticLogger.Initialize(config);
            
            // 记录应用程序启动日志
            ElasticLogger.Info("应用程序已启动");
        }

        /// <summary>
        /// 业务逻辑类示例
        /// </summary>
        public class UserService
        {
            public void CreateUser(string username)
            {
                try
                {
                    // 业务逻辑
                    Console.WriteLine($"创建用户: {username}");
                    
                    // 使用静态日志记录器记录信息
                    ElasticLogger.Info($"用户 {username} 创建成功", "UserService.CreateUser");
                }
                catch (Exception ex)
                {
                    // 记录错误日志
                    ElasticLogger.Error($"创建用户 {username} 失败", ex, "UserService.CreateUser");
                    throw;
                }
            }
        }

        /// <summary>
        /// 控制器类示例
        /// </summary>
        public class UserController
        {
            private readonly UserService _userService = new UserService();

            public void RegisterUser(string username)
            {
                try
                {
                    _userService.CreateUser(username);
                    
                    // 记录操作日志
                    ElasticLogger.Info($"用户注册请求处理完成: {username}", "UserController.RegisterUser");
                }
                catch (Exception ex)
                {
                    // 记录异常日志
                    ElasticLogger.Error($"用户注册失败: {username}", ex, "UserController.RegisterUser");
                }
            }
        }
    }
}